package com.qiguliuxing.dts.wx.controller;

import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.annotation.LoginUser;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.core.support.JacksonUtil;
import com.qiguliuxing.dts.core.validator.Sort;
import com.qiguliuxing.dts.wx.service.WxOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/wx/order")
@Validated
@Api(tags = "订单服务")
public class WxOrderController {
    private static final Logger logger = LoggerFactory.getLogger(WxOrderController.class);

    @Autowired
    private WxOrderService wxOrderService;

    /**
     * 订单列表
     *
     * @param userId   用户ID
     * @param showType 订单信息
     * @param page     分页页数
     * @param size     分页大小
     * @return 订单列表
     */
    @ApiOperation(value = "订单列表")
    @GetMapping("list")
    @WebLog(description = "订单列表", excludes = {LogParamsConsts.RESPONSE})
    public Object list(@LoginUser Integer userId,
                       @ApiParam(value = "订单信息", defaultValue = "0") @RequestParam(defaultValue = "0") Integer showType,
                       @ApiParam(value = "分页页数", defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
                       @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer size,
                       @RequestParam(value="topicId",required = false) Integer topicId
                     ) {
        return wxOrderService.list(userId, showType,topicId, page, size,"");
    }

    /**
     * 订单详情
     *
     * @param userId  用户ID
     * @param orderId 订单ID
     * @return 订单详情
     */
    @GetMapping("detail")
    @ApiOperation(value = "订单详情")
    @WebLog(description = "订单详情")
    public Object detail(@LoginUser Integer userId, @ApiParam(value = "订单ID") @NotNull Integer orderId) {
        return wxOrderService.detail(userId, orderId, false);
    }

    /**
     * 查看赠与订单详情
     *
     * @param userId  用户ID
     * @param orderId 订单ID
     * @return 订单详情
     */
    @ApiOperation(value = "查看赠与订单详情")
    @GetMapping("giftOrderDetail")
    @WebLog(description = "查看赠与订单详情")
    public Object giftOrderDetail(@LoginUser Integer userId, @ApiParam(value = "订单ID") @NotNull Integer orderId) {
        return wxOrderService.detail(userId, orderId, true);
    }


    /**
     * 赠予订单发起
     *
     * @param userId  用户ID
     * @param orderId 订单ID
     * @return 订单详情
     */
    @GetMapping("giftOrderSend")
    @ApiOperation(value = "赠予订单发起")
    @WebLog(description = "赠予订单发起")
    public Object giftOrderSend(@LoginUser Integer userId, @ApiParam(value = "订单ID") @NotNull Integer orderId) {
        return wxOrderService.giftOrderSend(userId, orderId);
    }

    /**
     * 赠予订单接收
     *
     * @param userId  用户ID
     * @param orderId 订单ID
     * @return 订单详情
     */
    @GetMapping("giftOrderReceive")
    @ApiOperation(value = "赠予订单接收")
    @WebLog(description = "赠予订单接收")
    public Object giftOrderReceive(@LoginUser Integer userId, @ApiParam(value = "订单ID") @NotNull Integer orderId) {
        return wxOrderService.giftOrderReceive(userId, orderId);
    }


    /**
     * 用户赠送订单列表
     *
     * @param userId   用户ID
     * @param giftType 显示类型，如果是1，则是当前用户赠送的订单；否则，则是当前用户收到的礼物订单
     * @return 赠送订单列表
     */
    @GetMapping("giftOrderlist")
    @ApiOperation(value = "用户赠送订单列表")
    @WebLog(description = "用户赠送订单列表", excludes = {LogParamsConsts.RESPONSE})
    public Object giftOrderlist(@LoginUser Integer userId,
                                @ApiParam(value = "显示类型", defaultValue = "1") @RequestParam(defaultValue = "1") Integer giftType,
                                @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
                                @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer size) {
        if (userId == null) {
            logger.error("查询用户赠送订单列表出错:用户未登录！！！");
            return ResponseUtils.unlogin();
        }
        return wxOrderService.giftOrderlist(userId, giftType, page, size);
    }

    /**
     * 物流跟踪
     *
     * @param userId
     * @param orderId
     * @return
     */
    @GetMapping("expressTrace")
    @ApiOperation(value = "物流跟踪")
    @WebLog(description = "物流跟踪")
    public Object expressTrace(@LoginUser Integer userId, @ApiParam(value = "订单ID") @NotNull Integer orderId) {
        return wxOrderService.expressTrace(userId, orderId);
    }

    /**
     * 提交订单
     *
     * @param userId 用户ID
     * @param body   订单信息，{ cartId：xxx, addressId: xxx, couponId: xxx, message: xxx,
     *               grouponRulesId: xxx, grouponLinkId: xxx}
     * @return 提交订单操作结果
     */
    @PostMapping("submit")
    @ApiOperation(value = "提交订单")
    @WebLog(description = "提交订单")
    public Object submit(@LoginUser Integer userId, @ApiParam(value = "{ cartId：xxx, addressId: xxx, couponId: xxx, message: xxx,grouponRulesId: xxx, grouponLinkId: xxx}") @RequestBody String body) {
        return wxOrderService.submit(userId, body);
    }

    /**
     * 验证自提配送
     *
     * @param userId 用户ID
     * @param body   订单信息，{ cartId：xxx}
     * @return 提交订单操作结果
     */
    @PostMapping("fetchVaild")
    @ApiOperation(value = "验证自提配送")
    @WebLog(description = "验证自提配送")
    public Object fetchVaild(@LoginUser Integer userId, @ApiParam(value = "{ cartId：xxx}") @RequestBody String body) {
        return wxOrderService.fetchVaild(userId, body);
    }

    /**
     * 取消订单
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 取消订单操作结果
     */
    @PostMapping("cancel")
    @ApiOperation(value = "取消订单")
    @WebLog(description = "取消订单")
    public Object cancel(@LoginUser Integer userId, @ApiParam(value = "{ orderId：xxx }") @RequestBody String body) {
        return wxOrderService.cancel(userId, body);
    }

    /**
     * 付款订单的预支付会话标识
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 支付订单ID
     */
    @PostMapping("prepay")
    @ApiOperation(value = "付款订单的预支付会话标识")
    public Object prepay(@LoginUser Integer userId, @ApiParam(value = "{ orderId：xxx }") @RequestBody String body, HttpServletRequest request) {
        return wxOrderService.prepay(userId, body, request);
    }

    /**
     * 微信付款成功或失败回调接口
     * <p>
     * TODO 注意，这里pay-notify是示例地址，建议开发者应该设立一个隐蔽的回调地址
     *
     * @param request  请求内容
     * @param response 响应内容
     * @return 操作结果
     */
    @PostMapping("dtsNotify")
    @ApiOperation(value = "微信付款成功或失败回调接口")
    public Object payNotify(HttpServletRequest request, HttpServletResponse response) {
        return wxOrderService.dtsPayNotify(request, response);
    }

    /**
     * 订单申请退款
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 订单退款操作结果
     */
    @PostMapping("refund")
    @ApiOperation(value = "订单申请退款")
    @WebLog(description = "订单申请退款")
    public Object refund(@LoginUser Integer userId, @ApiParam(value = "{ orderId：xxx }") @RequestBody String body) {
        return wxOrderService.refund(userId, body);
    }

    /**
     * 确认收货
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 订单操作结果
     */
    @PostMapping("confirm")
    @ApiOperation(value = "确认收货")
    @WebLog(description = "确认收货")
    public Object confirm(@LoginUser Integer userId, @ApiParam(value = "{ orderId：xxx }") @RequestBody String body) {
        return wxOrderService.confirm(userId, body);
    }

    /**
     * 删除订单
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 订单操作结果
     */
    @PostMapping("delete")
    @ApiOperation(value = "删除订单")
    @WebLog(description = "删除订单")
    public Object delete(@LoginUser Integer userId, @ApiParam(value = "{ orderId：xxx }") @RequestBody String body) {
        return wxOrderService.delete(userId, body);
    }

    /**
     * 待评价订单商品信息
     *
     * @param userId  用户ID
     * @param orderId 订单ID
     * @param goodsId 商品ID
     * @return 待评价订单商品信息
     */
    @GetMapping("goods")
    @ApiOperation(value = "待评价订单商品信息")
    @WebLog(description = "待评价订单商品信息")
    public Object goods(@LoginUser Integer userId, @ApiParam(value = "订单ID") @NotNull Integer orderId, @ApiParam(value = "商品ID") @NotNull Integer goodsId) {
        return wxOrderService.goods(userId, orderId, goodsId);
    }

    /**
     * 评价订单商品
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 订单操作结果
     */
    @PostMapping("comment")
    @ApiOperation(value = "评价订单商品")
    @WebLog(description = "评价订单商品")
    public Object comment(@LoginUser Integer userId, @ApiParam(value = "{ orderId：xxx }") @RequestBody String body) {
        return wxOrderService.comment(userId, body);
    }

    /**
     * 可退款订单详情
     *
     * @param userId  用户ID
     * @param orderId 订单ID
     * @return 订单详情
     */
    @GetMapping("preOrderRefund")
    @ApiOperation(value = "可退款订单详情")
    @WebLog(description = "可退款订单详情")
    public Object preOrderRefund(@LoginUser Integer userId, @ApiParam(value = "订单ID") @NotNull Integer orderId) {
        return wxOrderService.preOrderRefund(userId, orderId);
    }

    /**
     * 订单退货申请
     *
     * @param userId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 订单操作结果
     */
    @PostMapping("refundApply")
    @ApiOperation(value = "订单退货申请")
    @WebLog(description = "订单退货申请")
    public Object refundApply(@LoginUser Integer userId, @ApiParam(value = "{ orderId：xxx }") @RequestBody String body) {
        return wxOrderService.refundApply(userId, body);
    }

    /**
     * 退货单详情查询
     *
     * @param userId
     * @param refundId
     * @return
     */
    @GetMapping("refundOrderQuery")
    @ApiOperation(value = "退货单详情查询")
    @WebLog(description = "退货单详情查询")
    public Object refundOrderQuery(@LoginUser Integer userId, @ApiParam(value = "退货单ID") @NotNull Integer refundId) {
        return wxOrderService.refundOrderQuery(userId, refundId);
    }

    /**
     * 退货单撤销
     *
     * @param userId
     * @param body
     * @return
     */
    @PostMapping("refundUndo")
    @ApiOperation(value = "退货单撤销")
    @WebLog(description = "退货单撤销")
    public Object refundUndo(@LoginUser Integer userId, @ApiParam(value = "{refundId:xxx}") @RequestBody String body) {
        Integer refundId = JacksonUtil.parseInteger(body, "refundId");
        return wxOrderService.refundUndo(userId, refundId);
    }

    /**
     * 补录退款单快递信息
     *
     * @param userId
     * @param body
     * @return
     */
    @PostMapping("addFreightMsg")
    @ApiOperation(value = "补录退款单快递信息")
    @WebLog(description = "补录退款单快递信息")
    public Object addFreightMsg(@LoginUser Integer userId, @ApiParam(value = "{refundId:xxx,freightMsg:xxx}") @RequestBody String body) {
        Integer refundId = JacksonUtil.parseInteger(body, "refundId");
        String freightMsg = JacksonUtil.parseString(body, "freightMsg");
        return wxOrderService.addFreightMsg(userId, refundId, freightMsg);
    }

}
